package clue.editor;

import java.util.Stack;

import objects.BoardData;

public class GlobalHistory {

	private static GlobalHistory globalHistory;
	
	private Stack<BoardData> undoStack;
	
	private Stack<BoardData> redoStack;

	private BoardData previousVersion;
	
	private boolean disabled = false;
	
	public void initHistory(BoardData boardData)
	{
		previousVersion = boardData;
	}
	
	public void resetHistory()
	{
		undoStack.clear();
		redoStack.clear();
	}
	
	private GlobalHistory()
	{
		undoStack = new Stack<BoardData>();
		redoStack = new Stack<BoardData>();
	}
	
	public void addEdit(BoardData boardData)
	{
		if (!disabled)
		{
			undoStack.push(previousVersion);
			previousVersion = boardData;
			redoStack.clear();
		}
	}
	
	public void pushToRedo(BoardData boardData)
	{
		redoStack.push(boardData);
	}
	
	public void pushToUndo()
	{
		undoStack.push(previousVersion);
	}
	
	public BoardData popRedo()
	{
		undoStack.push(previousVersion);
		previousVersion = redoStack.pop();
		return previousVersion;
	}
	
	public BoardData popUndo()
	{
		redoStack.push(previousVersion);
		previousVersion = undoStack.pop();
		return previousVersion;
	}
	
	public static GlobalHistory getInstance()
	{
		if (globalHistory == null)
		{
			globalHistory = new GlobalHistory();
		}
		
		return globalHistory;
	}

	public boolean isRedoable() {
		return redoStack.size() > 0 ? true : false;
	}

	public boolean isUndoable() {
		return undoStack.size() > 0 ? true : false;
	}

	public boolean isDisabled() {
		return disabled;
	}

	public void setUndoStack(Stack<BoardData> undoStack) {
		this.undoStack = undoStack;
	}

	public void setRedoStack(Stack<BoardData> redoStack) {
		this.redoStack = redoStack;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public void setPreviousVersion(BoardData previousVersion) {
		this.previousVersion = previousVersion;
	}
	
	
}

